package com.p2p.quartz;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.p2p.account.po.Agent;
import com.p2p.account.service.AgentService;
import com.p2p.bill.service.BillService;
import com.p2p.common.cache.CacheManager;
import com.p2p.common.redis.RedisService;
import com.p2p.common.redis.vo.RedisVo;
import com.p2p.common.utils.StringUtils;
import com.p2p.terminal.po.AreaInfo;
import com.p2p.terminal.service.AreaInfoService;
import com.p2p.wallet.po.Wallet;
import com.p2p.wallet.service.CoinStartService;
import com.p2p.wallet.service.WalletService;

import net.sf.json.JSONObject;

public class StartJob {
	@Autowired
	private AreaInfoService areaInfoService;
	@Resource(name = "redisService")
	private RedisService<RedisVo> redisService;
	@Resource
	protected RedisTemplate redisTemplate;
	@Autowired
	private AgentService agentService;
	@Autowired
	private BillService billService;
	@Autowired
	private CoinStartService coinService;

	@Autowired
	private WalletService walletService;

	// 启动任务
	public void start() {

		CacheManager cacheManager = CacheManager.getInstance();
		// 缓存区域表
		cacheAreaTable(cacheManager);

		cacheAgentIdTable(cacheManager);

		cacheWalletInfo(cacheManager);

		Date date = new Date();
		Date date1 = new Date(date.getTime() - 24 * 60 * 60 * 1000);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:00:00");
		String str = format.format(date1);
		// billService.putbill(str);
	}

	private void cacheWalletInfo(CacheManager cacheManager) {
		List<Wallet> portList = walletService.selectList(new EntityWrapper<Wallet>().eq("status", 1));
		portList.forEach(item->{
			redisTemplate.opsForHash().put("p2p:walletInfo", item.getCoinId(), item.getName());
		});
		
	}


	// 代理商缓存
	private void cacheAgentIdTable(CacheManager cacheManager) {
		List<Agent> lists = agentService.selectList(new EntityWrapper<Agent>().eq("status", 1));

		HashMap<String, Set<Long>> map = new HashMap<String, Set<Long>>();

		lists.stream().filter(item -> item.getAgentLevel() == 3).forEach(item -> {
			HashSet<Long> hashSet = new HashSet<>();
			hashSet.add(Long.parseLong(item.getAgentCode()));
			map.put(item.getAgentCode(), hashSet);
		});

		lists.stream().filter(item -> item.getAgentLevel() == 2).forEach(item -> {
			Long agentId = Long.parseLong(item.getAgentCode());
			HashSet<Long> hashSet = new HashSet<>();
			hashSet.add(agentId);
			lists.stream().filter(item2 -> agentId.equals(item2.getTowLevelId())).forEach(b -> {

				hashSet.add(Long.parseLong(b.getAgentCode()));

			});
			map.put(item.getAgentCode(), hashSet);
		});

		lists.stream().filter(item -> item.getAgentLevel() == 1).forEach(item -> {
			Long agentId = Long.parseLong(item.getAgentCode());
			HashSet<Long> hashSet = new HashSet<>();
			hashSet.add(agentId);

			lists.stream().filter(item2 -> agentId.equals(item2.getOneLevelId())).forEach(b -> {
				hashSet.add(Long.parseLong(b.getAgentCode()));

			});
			map.put(item.getAgentCode(), hashSet);
		});

		cacheManager.addCache("agent_message", map);
	}

	private void cacheAreaTable(CacheManager cacheManager) {
		List<AreaInfo> lists = areaInfoService.queryAll();
		HashMap<String, String> map = new HashMap<String, String>();

		for (AreaInfo areaInfo : lists) {
			map.put(areaInfo.getAreaCode(), areaInfo.getAreaName());
		}
		cacheManager.addCache("area_message", map);
	}

}
